﻿using TradePublish.Common;
using log4net;
using TradePublish.Service;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Threading;
using TradePublish.Entity;
using TradePublish.Service;
namespace TradePublish.Dao
{
    public class ApplyUserContractDao
    {
        private ILog log = null;

        public ApplyUserContractDao()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public bool hasGrasped(string applyUserName)
        {
            string commandText = " select count(ID) from APPLY_USER_CONTRACT  where APPLY_USER_NAME='" + applyUserName.Replace("'", "''") + "'";
            DataSet set = new DataSet();
            while (true)
            {
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30)
                    {
                        Thread.Sleep(180000);//3fenzhong 
                        continue;
                    }
                    set = dbh.ExecuteDataSet(commandText); 
                    if (set.Tables != null && set.Tables.Count > 0 && set.Tables[0].Rows.Count > 0 && Convert.ToInt32(set.Tables[0].Rows[0][0]) > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception exception)
                {
                    this.log.Error("验证某申请人是否已采集，发生异常", exception);
                    Thread.Sleep(0x2710);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public void saveContract(List<ApplyUserContract> results)
        {
            if ((results != null) && (results.Count != 0))
            {
                while(true){
                    if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30)
                    {
                        Thread.Sleep(180000);//3fenzhong 
                        continue;
                    }
                    break;
                } 
                while (true)
                {
                    DatabaseHelper dbh = new DatabaseHelper();
                    try
                    { 
                        dbh.BeginTransaction();
                        for (int i = 0; i < results.Count; i++)
                        {
                            string cmdText = "INSERT INTO  APPLY_USER_CONTRACT  (id,APPLY_USER_NAME,EMAIL,EMAIL_SOURCE_HTML,TELEPHONE,TELEPHONE_SOURCE_HTML,WANGWANG_NO,WANGWANG_SOURCE_HTML,IS_CHENGXINTONG,OFFICAL_WEBSITE,HAVE_OFFICE_WEBSITE,HAVE_EMAIL,HAVE_TELEPHONE,HAVE_WANGWANG)";
                            string str2 = cmdText;
                            cmdText = str2 + " VALUES (gen_sequence.nextval,'" + results[i].applyUserName.Replace("'", "''") +
                                "','" + results[i].email.Replace("'", "''") +
                                "','" + results[i].emailSourceHtml.Replace("'", "''") +
                                "','" + results[i].telephone.Replace("'", "''") +
                                "','" + results[i].telephoneSourceHtml.Replace("'", "''") +
                                "','" + results[i].wangwangNo.Replace("'", "''") +
                                "','" + results[i].wangwangHtml.Replace("'", "''") +
                                "'," + ((results[i].isChengxintong == null || results[i].isChengxintong.Equals("")) ? "0" : results[i].isChengxintong) +
                                ",'" + results[i].officalWebsite.Replace("'", "''") + "'," + results[i].haveOfficeWebsite + "," + results[i].haveEmail + "," + results[i].haveTelephone + "," + results[i].haveWangwang+ 
                                ")";
                            this.log.Debug("<<<<<<<<<<<<<<" + cmdText);
                            dbh.ExecuteNonQuery(cmdText,TradePublish.Service.ConnectionState.KeepOpen);
                        }
                        dbh.CommitTransaction();
                        break;
                    }
                    catch (Exception exception)
                    {
                         dbh.RollbackTransaction();  
                        this.log.Error("存入APPLY_USER_CONTRACT时,发生异常.", exception);
                    }
                    finally
                    {
                        dbh.Dispose();
                    }
                    Thread.Sleep(0x3e8);
                } 
            }
        }
    }
}

